using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
public class UploadUtil
{
	private ArrayList arrayList_0 = new ArrayList();
	private Encoding encoding_0 = Encoding.UTF8;
	private string string_0 = string.Empty;
	public UploadUtil()
	{
		string str = DateTime.Now.Ticks.ToString("x");
		this.string_0 = "---------------------------" + str;
	}
	private byte[] get_data()
	{
		int num = 0;
		int num2 = 0;
		string s = "--" + this.string_0 + "--\r\n";
		byte[] bytes = this.encoding_0.GetBytes(s);
		this.arrayList_0.Add(bytes);
		foreach (byte[] array in this.arrayList_0)
		{
			num += array.Length;
		}
		byte[] array2 = new byte[num];
		foreach (byte[] array3 in this.arrayList_0)
		{
			array3.CopyTo(array2, num2);
			num2 += array3.Length;
		}
		return array2;
	}
	public bool upload(string url, ref string out_log)
	{
		WebClient webClient = new WebClient();
		webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + this.string_0);
		byte[] data = this.get_data();
		byte[] array;
		try
		{
			array = webClient.UploadData(url, data);
			out_log = Encoding.UTF8.GetString(array);
			return true;
		}
		catch (WebException ex)
		{
			Stream responseStream = ex.Response.GetResponseStream();
			array = new byte[ex.Response.ContentLength];
			responseStream.Read(array, 0, array.Length);
		}
		out_log = Encoding.UTF8.GetString(array);
		return false;
	}
	public void add_field(string name, string value)
	{
		string s = string.Format("--" + this.string_0 + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n", name, value);
		this.arrayList_0.Add(this.encoding_0.GetBytes(s));
	}
	public void add_file(string field_name, string file_name, string file_type)
	{
		FileStream fileStream = new FileStream(file_name, FileMode.Open, FileAccess.Read);
		byte[] array = new byte[fileStream.Length];
		fileStream.Read(array, 0, array.Length);
		fileStream.Close();
		int num = file_name.LastIndexOf("\\");
		string file_name2 = file_name.Substring(num + 1);
		this.add_file(field_name, file_name2, file_type, array);
	}
	public void add_file(string field_name, string file_name, string file_type, byte[] byte_0)
	{
		string s = "\r\n";
		string s2 = string.Format("--" + this.string_0 + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", field_name, file_name, file_type);
		byte[] bytes = this.encoding_0.GetBytes(s2);
		byte[] bytes2 = this.encoding_0.GetBytes(s);
		byte[] array = new byte[bytes.Length + byte_0.Length + bytes2.Length];
		bytes.CopyTo(array, 0);
		byte_0.CopyTo(array, bytes.Length);
		bytes2.CopyTo(array, bytes.Length + byte_0.Length);
		this.arrayList_0.Add(array);
	}
}
